﻿var sql = require( 'node-sqlserver' );
var dbContext = require( "../DataAccess/dbContext" );


/*
 * 通过员工工号获取基本信息
 */
exports.GetUserInfoByEmployeeNo = function ( EmployeeNo, successCall ) {

    dbContext.openHRConn( function ( conn ) {

        conn.query( "select * from EmployeeBasicInfo where EmployeeNo = ?",
            [EmployeeNo],
            function ( err, rows ) {

                if ( err ) {
                    console.log( "Select in DB Error: " + err );
                }

                var result = null;

                if ( rows != null && rows.length > 0 ) {
                    result = rows[0];
                }

                if ( successCall && typeof successCall == "function" ) { successCall( result ); }

            });
    });

};

/*
 * 通过EmpID获取员工基本信息
 */
exports.GetEmployeeInfoByEmpID = function ( EmpID, successCall ) {

    dbContext.openHRConn( function ( conn ) {

        conn.query( "select * from EmployeeBasicInfo where Id = ?", [EmpID], function ( err, rows ) {

            if ( err ) {
                console.log( "Select in DB Error: " + err );
            }

            var result = null;

            if ( rows != null && rows.length > 0 ) {
                result = rows[0];
            }

            if ( typeof successCall == "function" ) {
                successCall( result );
            }

        });

    });

};

/*
 * 登录验证
 */
exports.Authen = function ( adaccount, password, successCall ) {
    //if find return EmployeeId
    //else return false
    dbContext.openHRConn( function ( conn ) {
        conn.query( "select * from [user] where ADAccount = ? and ADPassword = ?",
            [adaccount, password],
            function ( err, rows ) {

                if ( err ) {
                    console.log( "Select in DB Error: " + err );
                }

                var result = 0;
                if ( rows != null && rows.length > 0 ) {

                    result = rows[0]["EmployeeId"];
                }

                if ( typeof successCall == "function" ) { successCall( result ); }

            });
    });

    return false;
};

/*
 * 根据工号获取用户菜单
 */
exports.GetMenuByUser = function ( EmployeeNo ) {

    return [
        { name: '首页', link: '#' },
        { name: '休假', link: '/Vacation/List' },
        { name: '入职', link: '#' }
    ];
};


/*
 * 获取用户子菜单
 */
exports.GetSubMenuByUser = function ( EmployeeNo ) {

    return [
        { name: '招聘申请', link: '#', img: '/images/top_img_1.gif' },
        { name: '录用申请', link: '#', img: '/images/top_img_2.gif' },
        { name: '业绩考核', link: '#', img: '/images/top_img_3.gif' }
    ];
};

/*
 * 获取我的待办
 */
exports.GetMyApproval = function ( EmployeeNo ) {

    return [
        { EmployeeNo: "VC1029202", ApplicationNO: '9382920244', TaskType: '新员工入职', CreateTime: '2014-02-20', Status: '' },
        { EmployeeNo: "VC1029202", ApplicationNO: '403923737', TaskType: '招聘申请', CreateTime: '2014-02-20', Status: '' },
        { EmployeeNo: "VC1029202", ApplicationNO: '10008376', TaskType: '业绩考勤', CreateTime: '2014-02-20', Status: '' }
    ];
};


/*
 * 获取我的申请单
 */
exports.GetMyApplcations = function ( EmployeeNo ) {

    return [
        { TaskType: '请假申请', CreateTime: '2014-02-20', Status: '申请中' },
        { TaskType: '请假申请', CreateTime: '2014-02-20', Status: '申请中' },
        { TaskType: '请假申请', CreateTime: '2014-02-20', Status: '申请通过' },
        { TaskType: '请假申请', CreateTime: '2014-02-20', Status: '申请中' }
    ];
};

/*
 * 获取员工直接主管
 */
exports.GetMyReportTo = function ( EmpID, successCall ) {

    dbContext.openHRConn( function ( conn ) {

        conn.query( "select ReportTo from jobinfo where Empid = ?", [EmpID], function ( err, rows ) {

            if ( err ) {
                err = "GetMyReportTo:" + err;
                console.log( "Select in DB Error: " + err );
            }

            var result = null;

            if ( rows != null && rows.length > 0 ) {
                result = rows[0]["ReportTo"];
            }

            if ( typeof successCall == "function" ) {
                successCall( result );
            }

        });

    });
}